<?php

namespace addons\HuaFeng\backend\controllers;

use common\helpers\ResultHelper;
use Yii;
use addons\HuaFeng\common\models\Procure;
use common\traits\Curd;
use common\models\base\SearchModel;
use yii\web\Controller;

/**
* Procure
*
* Class ProcureController
* @package addons\HuaFeng\backend\controllers
*/
class ProcureController extends BaseController
{
    use Curd;

    /**
    * @var Procure
    */
    public $modelClass = Procure::class;

    /**
    * 首页
    *
    * @return string
    * @throws \yii\web\NotFoundHttpException
    */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => [], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }
    public function actionOrder(){
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => [], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }

    /**
     * 批量通过采购审批
     *
     * @param $id
     * @return mixed
     */
    public function actionApproved(){
        $ids = Yii::$app->request->post('ids', []);
        if (empty($ids)) {
            return ResultHelper::json(422, '请至少选择一个采购单');
        }
        Procure::updateAll(['status' => 2], ['in', 'id', $ids]);
        return ResultHelper::json(200, '批量操作成功');
    }
}
